//
//  CalendarView.swift
//  GOFM
//
//  Created by siri on 2024/10/21.
//  Copyright © 2024 Glad. All rights reserved.
//

import SwiftUI


struct CalendarView: View {
      var columns: [GridItem] = [
          GridItem(.fixed(50)),
          GridItem(.fixed(50)),
          GridItem(.fixed(50)),
          GridItem(.fixed(50)),
          GridItem(.fixed(50)),
          GridItem(.fixed(50)),
          GridItem(.fixed(50))
       ]
    
    
       let weekArray = ["日","一","二","三","四","五","六"]
       var  statusCode:Int // 展开还是搜索状态
       var currentDay:String = String(Date().getCurrentDay()) // 获取当天日期
    
       @Binding var selectMonth:String // 选择的日期
       @Binding var selectYear:String // 选择的年份
       @State var selectDate:Int = -1 // 选择的日期
        
    
    

       // 获取当月有多少天
       var numDays:Int {
           return Date.getNumDays(year:  selectYear, month: "0\(selectMonth)")
       }
        
       // 获取当月第一天是星期几
       var firstDayWeek:Int {
           return Date.getWeekDayByFirstDayAndMonth(year:  selectYear, month: "0\(selectMonth)")
       }
    
    
       var body: some View {
           VStack(spacing: 0) {
               // 星期一至星期日 文字
               LazyVGrid(columns: columns, spacing: 10) {
                   ForEach(0..<7) { index in
                       Rectangle()
                           .fill(.white)
                           .frame(height: 30)
                           .overlay(
                            Text("\(weekArray[index])")
                                .font(Font.g_font_body_regular16)
                                .foregroundColor(Color.g_color_font_secondary)
                           )
                   }
               }.background(Color.g_color_bg_white).transition(.opacity)
               
               // 收缩状态
               if statusCode == 1 {
                   LazyVGrid(columns: columns, spacing: 10) {
                       ForEach(Date.getCurrentWeekDays()[0]..<Date.getCurrentWeekDays().last!+1,id:\.self) { index in
                           if (index == Int(currentDay) ) {
                               DateCircle(stateCode: 0, index: index,selectDate: $selectDate)
                           }else {
                               DateCircle(stateCode: 1, index: index,selectDate: $selectDate)
                           }
                       }
                   }.background(Color.g_color_bg_white).transition(.opacity)
               }else {
                   // 展开状态
                   LazyVGrid(columns: columns, spacing: 10) {
                       ForEach(-firstDayWeek+1...numDays,id:\.self) { index in
                           if index <= 0 {
	                           VStack{
                               }
                           }
                           // 如果是当前日期
                           else if index == Int(currentDay) {
                               DateCircle(stateCode: 0, index: index,selectDate: $selectDate)
                           }
                           // 不是当前日期
                           else {
                               DateCircle(stateCode: 1, index: index,selectDate: $selectDate)
                           }
                       }
                   }.background(Color.g_color_bg_white)
                       .transition(.opacity)
               }
           }
       }
}

struct CalendarView_Previews: PreviewProvider {
    static var previews: some View {
        Text("fdas")
//        CalendarView(statusCode: 1,selectDate: $selectDate)
    }
}
